package com.wxw.getapp.adapter

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.safframework.log.L
import com.wxw.getapp.R
import com.wxw.getapp.utils.AppUtils
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by Administrator on 2018/11/17.
 */
class Setapplist(context: Context) : BaseAdapter() {
    var context: Context = context
    var data: ArrayList<File> = arrayListOf()

    fun setdata(items: Array<File>) {
        for (item in items){
            this.data.add(item)
        }
        notifyDataSetChanged()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var viewHolder: ViewHolder
        var view: View

        if (convertView == null) {
            view = View.inflate(context, R.layout.list_items, null)
            viewHolder = ViewHolder(view)
            view.setTag(viewHolder)
        } else {
            view = convertView
            viewHolder = view.getTag() as ViewHolder
        }


        viewHolder.app_img.setImageDrawable(AppUtils.showUninstallAPKIcon(context, data[position].path))
        viewHolder.item_test.text = data[position].name
        viewHolder.app_dizhi.text = AppUtils.ShowLongFileSzie(data[position].length())
        viewHolder.app_btn.setOnClickListener {
            val builder = AlertDialog.Builder(context)
//            builder.setIcon(R.mipmap.get)
//            builder.setTitle("")
            //    指定下拉列表的显示数据
            val cities = arrayOf("分享", "信息","删除")
            //    设置一个下拉的列表选择项
            builder.setItems(cities) { dialog, which ->
                when (which) {

                    0 -> {
                        val apkFile = data[position]
                        val intent = Intent()
                        intent.action = Intent.ACTION_SEND
                        intent.type = "*/*"
//                        intent.type = "application/vnd.android.package-archive"
                        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(apkFile))
                        context.startActivity(intent)
                    }
                    1 -> {
                        val time = SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
                                .format(Date(data[position].lastModified()))
                        showNormalDialog(data[position].name
                                ,AppUtils.ShowLongFileSzie(data[position].length())
                                ,AppUtils.showUninstallAPKIcon(context, data[position].path),data[position].path
                                ,time)

                    }
                    2 ->{
                        showdeleteDialog(data[position].name
                                ,data[position]
                                ,position)
                    }
                }
            }
            builder.show()
        }

        return view


    }

    override fun getItem(position: Int): Any {

        return data.size
    }

    override fun getItemId(position: Int): Long {

        return position.toLong()
    }

    override fun getCount(): Int {

        return data.size
    }

    class ViewHolder(var v: View) {
        var item_test: TextView = v.findViewById(R.id.app_name)
        var app_img: ImageView = v.findViewById(R.id.app_img)
        var app_dizhi: TextView = v.findViewById(R.id.app_dizhi)
        var app_btn: Button = v.findViewById(R.id.app_btn)
    }

    private fun showNormalDialog(appName: String, appSize: String ,appIcon:Drawable,appPath:String,time:String) {
        /*
        @setIcon 设置对话框图标
                *
        @setTitle 设置对话框标题
                *
        @setMessage 设置对话框消息提示
                *
        setXXX方法返回Dialog对象，因此可以链式设置属性
                */
        val normalDialog = AlertDialog.Builder(context)
        normalDialog.setIcon(appIcon)
        normalDialog.setTitle(appName)
        normalDialog.setMessage("大小:" + appSize+"\n路径:"+appPath+"\n创建日期:"+time)
        normalDialog.setPositiveButton("确定"
        ) { dialog, which ->
            //...To-do
        }
        //        normalDialog.setNegativeButton("关闭",
        //                new DialogInterface.OnClickListener()
        //                {
        //                    @Override
        //                    public void onClick(DialogInterface
        //                                                dialog, int which)
        //                    {
        //                        //...To-do
        //                    }
        //                });
        //显示
        normalDialog.show()
    }

    private fun showdeleteDialog(appName: String,appFile: File,position:Int) {
        /*
        @setIcon 设置对话框图标
                *
        @setTitle 设置对话框标题
                *
        @setMessage 设置对话框消息提示
                *
        setXXX方法返回Dialog对象，因此可以链式设置属性
                */
        val normalDialog = AlertDialog.Builder(context)
        if (Build.VERSION.SDK_INT > 20) {
            normalDialog.setIcon(R.mipmap.logo)
        }
        normalDialog.setTitle("警告！")
        normalDialog.setMessage("确定删除"+appName+"?")
        normalDialog.setPositiveButton("确定") { dialog, which ->
            //...To-do
            if (appFile.exists())
            AppUtils.deleteLocal(appFile)
            data.removeAt(position)
            notifyDataSetChanged()
        }
        normalDialog.setNegativeButton("取消") { dialog, which ->
            //...To-do
        }
        //显示
        normalDialog.show()
    }
}